package com.sann.eduservice.controller;


import com.sann.common.utils.R;
import com.sann.eduservice.entity.course.Course;
import com.sann.eduservice.entity.course.vo.CourseInfoVo;
import com.sann.eduservice.entity.course.vo.CoursePublishVO;
import com.sann.eduservice.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2021-04-17
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/eduservice/course/")
@CrossOrigin
public class CourseController {
    @Autowired
    private CourseService courseService;

    // TODO  完善条件: 仿照讲师模块查询带分页
    @ApiOperation(value = "查询课程列表")
    @GetMapping(value = "getList")
    public R getList(){
        List<Course> list = this.courseService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "添加课程")
    @PostMapping(value = "insert")
    public R save(@ApiParam(name="courseInfoVo",value = "课程基本信息", required = true)
                    @RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程cid，为了后面添加大纲使用
        String cid = courseService.insert(courseInfoVo);
        if (!StringUtils.isEmpty(cid)) {
            return R.ok().data("courseId", cid);
        }else {
            return R.error().msg("保存课程信息失败");
        }
    }

    @ApiOperation(value = "根据课程ID查询课程基本信息", notes = "返回的是课程VO对象")
    @GetMapping(value = "getCourseInfoById/{courseId}")
    public R getCourseInfoById(@ApiParam(name = "courseId", value = "课程ID")
                     @PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfoVoById(courseId);

        return R.ok().data("courseInfoVo", courseInfoVo);
    }


    @ApiOperation(value = "修改课程信息", notes = "根据ID修改课程信息")
    @PostMapping(value = "updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        int result = courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "发布课程信息", notes = "根据课程courseId获取发布课程信息")
    @GetMapping(value = "getCoursePublishById/{courseId}")
    public R getCoursePublishById(@PathVariable String courseId){
        CoursePublishVO cousePublishVO = this.courseService.getCoursePublishById(courseId);
        return R.ok().data("coursePublishVO", cousePublishVO);
    }

    @ApiOperation(value = "执行发布课程", notes = "根据课程courseId更新课程表")
    @PostMapping(value = "publishCourseByCourseId/{courseId}")
    public R publishCourseByCourseId(@PathVariable String courseId){
        boolean bol = courseService.publishCourseByCourseId(courseId);
        if (bol) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping(value = "delete/{courseId}")
    public R delete(@PathVariable("courseId") String courseId){
        this.courseService.delete(courseId);
        return R.ok();
    }

}

